package com.example.myys.ui.page.syPage

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Card
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.example.myys.data.entity.DataBean
import com.example.myys.ui.Resource
import com.example.myys.ui.StateHandler
import com.example.myys.ui.base.ListItem
import com.example.myys.viewModel.syModel.SyViewModel

//首页展示
@Composable
fun Sy(syViewModel: SyViewModel = SyViewModel(), modifier: Modifier) {
    val ysState by syViewModel.ysState.collectAsState()
    val dsjState by syViewModel.dsjState.collectAsState()
    val zyState by syViewModel.zyState.collectAsState()
    val dmState by syViewModel.dmState.collectAsState()
    val djState by syViewModel.djState.collectAsState()
    val xsState by syViewModel.xsState.collectAsState()

    Column(
        modifier = Modifier
            .fillMaxSize()
            .verticalScroll(rememberScrollState()),
        verticalArrangement = Arrangement.Top,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Card(
            modifier = Modifier
                .fillMaxWidth()
                .padding(5.dp)
                .height(270.dp),
        ) {
            Text(text = "推荐", modifier = Modifier.padding(10.dp))
            StateHandler(state = ysState) { resource ->
                resource.data?.let { ysList ->
                    LazyRow(
                        modifier = Modifier.padding(10.dp, 0.dp),
                        horizontalArrangement = Arrangement.spacedBy(5.dp)
                    ) {
                        items(ysList) { item ->
                            ListItem(dataBean = item)
                        }
                    }
                }
            }
        }
        Card(
            modifier = Modifier
                .padding(5.dp)
                .fillMaxWidth()
                .height(700.dp)
        ) {
            Text(text = "排行榜", modifier = Modifier.padding(10.dp))
            LazyRow(modifier = Modifier.padding(10.dp, 0.dp)) {
                item(key = "电影") { YsPhbLb(name = "电影", ysState) }
                item(key = "电视剧") { YsPhbLb(name = "电视剧", dsjState) }
                item(key = "动漫") { YsPhbLb(name = "动漫", dmState) }
                item(key = "综艺") { YsPhbLb(name = "综艺", zyState) }
                item(key = "短剧") { YsPhbLb(name = "短剧", djState) }
                item(key = "小说") { YsPhbLb(name = "小说", xsState) }
            }
        }
    }
}

//排行榜列表
@Composable
fun YsPhbLb(
    name: String, ysState: Resource<List<DataBean>>,
    onClickItem: (dataBean: DataBean) -> Unit = {}, //点击事件
) {
    Card(
        modifier = Modifier
            .padding(2.dp)
            .width(300.dp)
    ) {
        Text(text = name)
        StateHandler(state = ysState) { resource ->
            resource.data?.let { ysList ->
                LazyColumn(
                    verticalArrangement = Arrangement.spacedBy(3.dp)
                ) {
                    itemsIndexed(ysList) { index, item ->
                        ListItem(index = index + 1, dataBean = item, onClickItem = {
                            onClickItem(it)
                        })
                    }
                }
            }
        }
    }
}